﻿using System;
using MySql.Simple.Mapper.Finders;
using MySql.Simple.Mapper.Structures;

namespace MySql.Simple.Mapper.Commands.Tables
{
    /// <summary>
    /// Reads the contents of a SQL table row into a given object type.
    /// </summary>
    public static class DropCommand
    {
        private const string Sql = @"DROP TABLE `{0}`;";

        /// <summary>
        /// Creates the table for the given type.
        /// </summary>
        /// <returns></returns>
        public static void Execute(Type type, Connection connection)
        {
            // Check table doesn't exist
            if (!ExistsCommand.Execute(type, connection))
            {
                throw new ApplicationException("Table doesn't exist for " + type.Name);
            }

            // Get SQL ingredients
            var tableName = TableNameFinder.Find(type);

            // build SQL statement
            connection.ExecuteNonQuery(Sql, null, tableName);
        }

        /// <summary>
        /// Creates the table for the given type.
        /// </summary>
        /// <returns></returns>
        public static void Execute<T>(Connection connection)
        {
            Execute(typeof(T), connection);
        }
    }
}